const mysql = require('mysql2')

const config = require('./config')

const connections = mysql.createPool({
    port: config.MYSQL_PORT,
    host: config.MYSQL_HOST,
    database: config.MYSQL_DATABASE,
    user: config.MYSQL_USER,
    password: config.MYSQL_PASSWORD
})

// 监听连接错误事件  
connections.on('error', (err) => {  
    console.error('数据库连接错误:', err)  
})   

connections.getConnection((err,conn) => {
    if (err) {
        console.log('连接失败');
    } else {
        console.log('数据库连接成功');
        conn.release()
    }
})

module.exports = connections.promise()